<?php defined('BASEPATH') or exit('No direct script access allowed');
/**
 * Public Bet module controller
 *
 * @author  PyroCMS Dev Team
 * @package PyroCMS\Core\Modules\Blog\Controllers
 */
class Bet extends L_Controller
{
	const MULTI_CHOICES_BET = 'MULTI_CHOICES_BET';
	const CONCATENATED_CHOICES_BET = 'CONCATENATED_CHOICES_BET';
	var $errors = array();
	
	var $bet_type_handler_mapping = array (
		'MN_X30' => self::MULTI_CHOICES_BET,
	    'MN_X150' => self::MULTI_CHOICES_BET, 
	    'MN_X750' => self::MULTI_CHOICES_BET,
	
		'MB_X12' => self::MULTI_CHOICES_BET,
		'MB_X45' => self::MULTI_CHOICES_BET,
		'MB_X150' => self::MULTI_CHOICES_BET,
	
		'MT_X30' => self::MULTI_CHOICES_BET,
		'MT_X150' => self::MULTI_CHOICES_BET,
		'MT_X750' => self::MULTI_CHOICES_BET,
	
		'MN_CLH_X1.9' => self::CONCATENATED_CHOICES_BET, 
		'MN_CLT_X1.9' => self::CONCATENATED_CHOICES_BET, 
		'MN_H_X8.5' => self::CONCATENATED_CHOICES_BET,
		'MN_H_X800' => self::CONCATENATED_CHOICES_BET,
		'MN_H_X85' => self::CONCATENATED_CHOICES_BET,
		'MN_T_X8.5' => self::CONCATENATED_CHOICES_BET,
		'MN_T_X800' => self::CONCATENATED_CHOICES_BET,
		'MN_T_X85' => self::CONCATENATED_CHOICES_BET,
		'MN_TXH_X1.9' => self::CONCATENATED_CHOICES_BET,
		'MN_TXT_X1.9' => self::CONCATENATED_CHOICES_BET,
		'MN_X400' => self::CONCATENATED_CHOICES_BET,
		'MN_X45' => self::CONCATENATED_CHOICES_BET,
		'MN_X450' => self::CONCATENATED_CHOICES_BET,
		'MN_X5.3' => self::CONCATENATED_CHOICES_BET,
		
		'MB_X3.5' => self::CONCATENATED_CHOICES_BET,
		'MB_X34' => self::CONCATENATED_CHOICES_BET,
		'MB_X400' => self::CONCATENATED_CHOICES_BET,
		'MB_X800' => self::CONCATENATED_CHOICES_BET,
		'MB_T_X8.5' => self::CONCATENATED_CHOICES_BET,
		'MB_X85' => self::CONCATENATED_CHOICES_BET,
		'MB_TX_X1.9' => self::CONCATENATED_CHOICES_BET,
		'MB_CL_X1.9' => self::CONCATENATED_CHOICES_BET,
		
		'MT_X5.3' => self::CONCATENATED_CHOICES_BET,
		'MT_X45' => self::CONCATENATED_CHOICES_BET,
		'MT_X450' => self::CONCATENATED_CHOICES_BET,
		'MT_H_X800' => self::CONCATENATED_CHOICES_BET,
		'MT_T_X800' => self::CONCATENATED_CHOICES_BET,
		'MT_X400' => self::CONCATENATED_CHOICES_BET,
		'MT_H_X8.5' => self::CONCATENATED_CHOICES_BET,
		'MT_T_X8.5' => self::CONCATENATED_CHOICES_BET,
		'MT_H_X85' => self::CONCATENATED_CHOICES_BET,
		'MT_T_X85' => self::CONCATENATED_CHOICES_BET,
		'MT_TXH_X1.9' => self::CONCATENATED_CHOICES_BET,
		'MT_TXT_X1.9' => self::CONCATENATED_CHOICES_BET,
		'MT_CLH_X1.9' => self::CONCATENATED_CHOICES_BET,
		'MT_CLT_X1.9' => self::CONCATENATED_CHOICES_BET
	);
	
	public function __construct()
	{
		parent::__construct();
		$this->load->model('bet_m');
		$this->load->model('channel/l_user_m');
		
		$this->load->model('channel/bet_type_m');
		
		$this->load->library('channel/liberty_reserver');
		$this->load->library('form_validation');
		
		if (!$this->ion_auth->logged_in()) {
			redirect('users/login');
			return;
		}
		
		$this->bind_request_data();
		$this->template
			 ->set('redirect_to', "")
			 ->set('errors', $this->errors)
			 ->set_partial('header', 'partials/header')
			 ->set_partial('right_panel', 'partials/right_panel')
			 ->set('balance_amount', $this->balance_amount);
	}
	
	private function bind_request_data() {
		$this->channel_id = $_REQUEST['channel_id'];
		$this->choices = $_REQUEST['choices'];
		$this->amount = $_REQUEST['amount'];
		$this->bet_type_id = $_REQUEST['bet_type_id'];
		$this->choices_count = sizeof($this->choices);
		
		$this->bet_type = $this->bet_type_m->get_by_id_with_cache($this->bet_type_id);
		
		$user_id = $this->session->userdata('id');
		$this->balance_amount = $this->l_user_m->get_account_balance($user_id);
	}
	public function index() {
	}
    public function handle_bet() {
    	$is_valid_handler = false;
    	if (isset($this->bet_type_handler_mapping[$this->bet_type->bet_type])) {
    		if ($this->bet_type_handler_mapping[$this->bet_type->bet_type] == self::MULTI_CHOICES_BET) {
				$this->process_multi_choises_bet();
				$is_valid_handler = true;
    		}
    		
    		if ($this->bet_type_handler_mapping[$this->bet_type->bet_type] == self::CONCATENATED_CHOICES_BET) {
				$this->process_concatenated_choises_bet();
				$is_valid_handler = true;
    		}
    	}
    	if (!$is_valid_handler) {
    		$this->template->build('bet/bet_error');
    	}
	}
	
	private function process_multi_choises_bet() {
		$is_valid_bet = $this->validate_multi_choices_bet($this->bet_type, $this->choices, $this->balance_amount, $this->amount);
		if (!$is_valid_bet) {
			$this->template
				 ->build('bet/bet_error');
		} else {
			$this->add_multi_choises_bet_transaction($this->bet_type_id, $this->channel_id, $this->choices, $this->amount);
			
			$this->render_success_multi_choices_bet($this->bet_type, $this->amount, $this->choices);
		}
	}
	
	private function process_concatenated_choises_bet() {
		$is_valid_bet = $this->validate_concatenated_choices_bet($this->bet_type, $this->choices_count, $this->balance_amount, $this->amount);
		
		if (!$is_valid_bet) {
			$this->template->build('bet/bet_error');
		} else {
			$this->add_concatenated_choises_bet_transaction($this->bet_type_id, $this->channel_id, $this->choices, $this->amount);
			
			$this->render_success_concatenated_choices_bet($this->bet_type, $this->amount, $this->choices);
		}
	}
	
	/*******************************************************
	 * Helper methods for multi choices bet                *
	 *******************************************************/
	private function add_multi_choises_bet_transaction($bet_type_id, $channel_id, $choices, $amount) {
		$multi_choices = $this->build_multi_choices($choices);
		$this->bet_m->add_bet_transaction($this->current_user->id, $bet_type_id, $channel_id, $multi_choices, $amount);
	}
	
	private function render_success_multi_choices_bet($bet_type, $amount, $choices) {
		$multi_choices = $this->build_multi_choices($choices);
		var_dump($this->errors);
		$this->template->set('bet_type_name', $bet_type->bet_type)
					 ->set('choices', array($multi_choices))
					 ->set('amount', $amount)
					 ->build('bet_success');
	}
	
	private function validate_multi_choices_bet($bet_type, $choices_count, $balance_amount, $bet_ammount) {
		if (!$this->is_valid_multi_choices_count($bet_type, sizeof($this->choices))) {
			return false;
		}
		if ($this->is_duplicated_choices($this->choices)) {
			$this->errors[] = "Số được chọn bị trùng lặp.";
			return false;
		}
		
		$this->form_validation->set_rules('amount', 'Amount-Minimum', 'required|greater_than_equal_to[1]');
		$this->form_validation->set_rules('channel_id', 'ChannelId', 'required|integer');
		$this->form_validation->set_rules('amount', 'Amount-Balance', 'required|less_than_equal_to[' . $balance_amount . ']');
		
		$this->form_validation->set_message('less_than_equal_to', 'Số dư tài khoản không đủ. số dư TK = ' . number_format($balance_amount, 1) . 'LR. Tiền cược = ' . number_format($bet_ammount, 1) . 'LR');
		return $this->form_validation->run();	
	}
	
	private function is_duplicated_choices($choices) {
		foreach ($choices as $i => $choice1) {
			foreach ($choices as $j => $choice2) {
				if ($i != $j && trim($choice1) == ($choice2)) {
					return true;
				}
			}
		}
		return false;
	}
	
	private function build_multi_choices($choices) {
		$multi_choises = '';
		foreach ($choices as $i => $choice) {
			if ($i == 0) {
				$multi_choises = $choice;
			} else {
				$multi_choises = $multi_choises . '-' . $choice;
			}
		}
		return $multi_choises;
	}
	
	private function is_valid_multi_choices_count($bet_type, $choices_count) {
		$bet_type_name = $bet_type->bet_type;
		$required_choice_count = $bet_type->choice_count;
		return $required_choice_count == $choices_count;
	}
	
	/*******************************************************
	 * Helper methods for concatenated choices bet         *
	 *******************************************************/
	private function add_concatenated_choises_bet_transaction($bet_type_id, $channel_id, $choices, $amount) {
		$multi_choices = $this->build_concatenated_choices($choices);
		$this->bet_m->add_bet_transaction($this->current_user->id, $bet_type_id, $channel_id, $multi_choices, $amount);
	}
	
	private function render_success_concatenated_choices_bet($bet_type, $amount, $choices) {
		$concatenated_choices = $this->build_concatenated_choices($choices);
		$this->template->set('bet_type_name', $bet_type->bet_type)
					 ->set('choices', array($concatenated_choices))
					 ->set('amount', $amount)
					 ->set('errors', $this->errors)
					 ->build('bet_success');
	}
	
	private function validate_concatenated_choices_bet($bet_type, $choices_count, $balance_amount, $bet_ammount) {
		if (!$this->is_valid_concatenated_choices_count($bet_type, $choices_count)) {
			return false;
		}
		
		$this->form_validation->set_rules('amount', 'Amount-Minimum', 'required|greater_than_equal_to[1]');
		$this->form_validation->set_rules('channel_id', 'ChannelId', 'required|integer');
		$this->form_validation->set_rules('amount', 'Amount-Balance', 'required|less_than_equal_to[' . $balance_amount . ']');
		
		$this->form_validation->set_message('less_than_equal_to', 'Số dư tài khoản không đủ. số dư TK = ' . number_format($balance_amount, 1) . 'LR. Tiền cược = ' . number_format($bet_ammount, 1) . 'LR');
		return $this->form_validation->run();	
	}
	
	private function build_concatenated_choices($choices) {
		$concatenated_choises = '';
		foreach ($choices as $i => $choice) {
				$concatenated_choises = $concatenated_choises .$choice;
		}
		return $concatenated_choises;
	}
	
	private function is_valid_concatenated_choices_count($bet_type, $choices_count) {
		$bet_type_name = $bet_type->bet_type;
		$required_choice_count = $bet_type->choice_count;
		return $required_choice_count == $choices_count;
	}
}
